<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Subscribe extends My_Controller {

    function __construct() {
        parent::__construct();
        $this->config->load('osc_config', TRUE);
    }

    public function index() {
        $this->load->helper('form');
        $this->load->model('Subscribe_model', 'subscribe');

        if ($this->input->post('Subscribe')) {
            die("asdas");
            $this->subscribe->subscribe_insert();
        }

        $this->navigation->loadHomeView($data);
    }

    public function insert() {

        $this->load->model('Subscribe_model', 'subscribe');
        $this->load->library('form_validation');
        $this->load->helper(array('form', 'url'));

        $posts = $this->input->post();

        $this->form_validation->set_rules('subscribe_name', 'FullName', 'required');
        $this->form_validation->set_rules('subscribe_email', 'Email', 'required');


        if ($this->form_validation->run() == FALSE) {
            $this->load->view('myform');
        } else {
            $this->subscribe->subscribe_insert($posts);
            redirect(site_url('home'));
            $this->navigation->loadHomeView($posts);
        }
        //var_dump($posts); 
        // die();          
        // redirect(site_url('controler/action/variable'));
        redirect(site_url('home'));
        $this->navigation->loadHomeView($posts);
    }

    public function subscribe_ajax() {
        $this->load->model('Subscribe_model', 'subscribe');
        $this->load->library('form_validation');
        $this->load->helper(array('form', 'url'));
        $email_sys_subscribe_site = $this->config->item('email_subscribe_site', 'osc_config');
        $name_sys_subscribe_site = $this->config->item('name_subscribe_site', 'osc_config');
        $email_subscribe_config = $this->config->item('email_subscribe_config', 'osc_config');

        $posts = $this->input->post();

        $this->form_validation->set_rules('subscribe_name', 'Full name ', 'trim|required|min_length[4]|max_length[25]');
        $this->form_validation->set_rules('subscribe_email', 'Email', 'trim|required|min_length[4]|max_length[40]|valid_email|is_unique[subscribe.subscribe_email]');

        if ($this->form_validation->run() === FALSE) {
            echo json_encode(array('error' => '1', 'message' => validation_errors('<div class="alert alert-error"><strong>Error!</strong> ', '</div>')));
        } else {
            //Collect your form data
            //$username = $this->input->post('username');
            //etc..
            //Save the data to the database, of course you will need all the data first.
            $this->subscribe->subscribe_insert($posts);
            /*
              // send mail config : config/osc_config.php

              $this->load->library('email', $email_subscribe_config);
              $this->email->set_newline("\r\n");
              $email_body = "<div>Your subscribed Go Gecko succesfull !</div>";
              $this->email->from($email_sys_subscribe_site, $name_sys_subscribe_site);
              $list = array($this->input->post('subscribe_email'));
              $this->email->to($list);
              $this->email->subject('Subscribe Go Gecko');
              $this->email->message($email_body);

              $this->email->send();
             * 
             */
            ///
            $emailFrom = $this->input->post('subscribe_email');
            $name = $this->input->post('subscribe_name');

            $subject = "Subscription request";
            $content = "<b>Name:</b> $name <br>
                    <b>Email:</b> $emailFrom";

            //sendmail($emailTo, $subject, $content, $emailFrom = null, $name = null, $mailtype = 'html')
            sendmail('', $subject, $content, $emailFrom, $name);
            // echo $this->email->print_debugger();
            /*
              $this->email->from($email_sys_subscribe_site, $name_sys_subscribe_site);
              $this->email->to($this->input->post('subscribe_email'));
              $this->email->subject('Subscribe Go Gecko');
              $this->email->message("Your subscribed Go Gecko succesfull !");
              $this->email->send();

             */
            //redirect(site_url('home'));
            //$this->navigation->loadHomeView($posts);
            //Send the success to our javascript file.
            echo json_encode(array('error' => '0', 'message' => '<div class="alert alert-success"><strong>Success!</strong> You have been registered.</div>'));
        }
    }

}

